package com.honor.games.wg.java;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.google.common.eventbus.EventBus;
import com.honor.games.wg.config.ApplicationConfiguration;
import com.honor.games.wg.events.BoardEventManager;
import com.honor.games.wg.events.DebugEventManager;
import com.honor.games.wg.events.UnitEventManager;
import com.honor.games.wg.graphics.WGGraphics;
import com.honor.games.wg.gui.DebugFrame;

public class WGDesktop {
	private static ApplicationContext applicationContext;
	
	private static Logger logger = LoggerFactory.getLogger(WGDesktop.class);

	private static EventBus eventBus;

	private static WGGraphics graphics; 

	public static void main (String[] args) {
		logger.trace("Main - START");
		
		applicationContext = new ClassPathXmlApplicationContext("service-context.xml");
		ApplicationConfiguration appConfig = applicationContext.getBean(ApplicationConfiguration.class);
		graphics = applicationContext.getBean(WGGraphics.class);
		eventBus = applicationContext.getBean(EventBus.class);
		eventBus.register(applicationContext.getBean(DebugEventManager.class));
		eventBus.register(applicationContext.getBean(UnitEventManager.class));
		eventBus.register(applicationContext.getBean(BoardEventManager.class));
		DebugFrame debugFrame = applicationContext.getBean(DebugFrame.class);
		debugFrame.addEventListeners(eventBus);
		debugFrame.setVisible(true);
		
		logger.trace("Creating new application {}", appConfig.getApplicationName());
		logger.trace("Version : {}", appConfig.getApplicationVersion());
		
		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
		config.useGL20 = true;
		config.width = appConfig.getScreenWidth();
		config.height = appConfig.getScreenHeight();
		new LwjglApplication(graphics, config);
		
		logger.trace("Main - STOP");
	}
}
